package hczc.longcai.com.hccrowdfunding.home.fourth_mine;


import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;
import hczc.longcai.com.hccrowdfunding.beans.UserInfo;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities.AttentionPeopleActivity;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities.AttentionProjectActivity;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities.InvestmentOrderActivity;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities.MineDataActivity;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities.MineSponsorActivity;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities.MineWalletActivity;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities.RewardOrdersActivity;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities.ScoreActivity;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities.SettingsActivity;
import hczc.longcai.com.hccrowdfunding.utils.httputils.LoadDatas;
import hczc.longcai.com.hccrowdfunding.views.CircleImageView;

/**
 * A simple {@link Fragment} subclass.
 */
public class FourthFragment extends Fragment implements View.OnClickListener {

    public final static int STATE_ALL = 0;
    public final static int STATE_DZF = 1;
    public final static int STATE_DFH = 2;
    public final static int STATE_DSH = 3;
    private static final String TAG = "FourthFragment";

    private UserInfo info;

    private int state = 0;

    private GetHead asyncTask;
    private CircleImageView head;
    private TextView name;


    public FourthFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onResume() {
        super.onResume();
        SharedPreferences mSharedPreferences = BaseApplication.mSharedPreferences;
        if (mSharedPreferences.getBoolean("isLogin", false)) {

            asyncTask = new GetHead();
            initDatas();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fourth, container, false);
        BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));


        initViews(view);
        return view;
    }

    private void initViews(View view) {
        head = ((CircleImageView) view.findViewById(R.id.mine_head));
        head.setOnClickListener(this);
        view.findViewById(R.id.mine_normal_sz).setOnClickListener(this);
        name = ((TextView) view.findViewById(R.id.mine_name));

        view.findViewById(R.id.mine_tabs_attention_project).setOnClickListener(this);
        view.findViewById(R.id.mine_tabs_attention_people).setOnClickListener(this);
        view.findViewById(R.id.mine_tabs_myfans).setOnClickListener(this);
        view.findViewById(R.id.mine_tabs_score).setOnClickListener(this);

        view.findViewById(R.id.mine_rewardorders_all_layout).setOnClickListener(this);
        view.findViewById(R.id.mine_rewardorders_dzf_layout).setOnClickListener(this);
        view.findViewById(R.id.mine_rewardorders_dfh_layout).setOnClickListener(this);
        view.findViewById(R.id.mine_rewardorders_dsh_layout).setOnClickListener(this);

        view.findViewById(R.id.mine_normal_tzdd).setOnClickListener(this);
        view.findViewById(R.id.mine_normal_wdfq).setOnClickListener(this);
        view.findViewById(R.id.mine_normal_wdqb).setOnClickListener(this);

    }

    private void initDatas() {

        asyncTask.execute(((Void) null));


    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        Intent intent = new Intent(getActivity(), RewardOrdersActivity.class);
        Bundle bundle = new Bundle();
        switch (id) {
            case R.id.mine_head:
                Intent mineData = new Intent(getActivity(), MineDataActivity.class);
                Bundle mine = new Bundle();
                mine.putSerializable("userinfo", info);
                mineData.putExtras(mine);
                startActivity(mineData);
                break;
            case R.id.mine_normal_sz:
                getActivity().startActivityForResult(new Intent(getActivity(), SettingsActivity.class),1);
                break;
            case R.id.mine_tabs_attention_project:
                startActivity(new Intent(getActivity(), AttentionProjectActivity.class));
                break;
            case R.id.mine_tabs_attention_people:
                startActivity(new Intent(getActivity(), AttentionPeopleActivity.class));
                break;
            case R.id.mine_tabs_score:
                startActivity(new Intent(getActivity(), ScoreActivity.class));
                break;
            case R.id.mine_rewardorders_all_layout:
                state = STATE_ALL;
                bundle.putInt("state", state);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.mine_rewardorders_dzf_layout:
                state = STATE_DZF;
                bundle.putInt("state", state);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.mine_rewardorders_dfh_layout:
                state = STATE_DFH;
                bundle.putInt("state", state);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.mine_rewardorders_dsh_layout:
                state = STATE_DSH;
                bundle.putInt("state", state);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.mine_normal_tzdd:
                startActivity(new Intent(getActivity(), InvestmentOrderActivity.class));
                break;
            case R.id.mine_normal_wdfq:
                startActivity(new Intent(getActivity(), MineSponsorActivity.class));
                break;
            case R.id.mine_normal_wdqb:
                startActivity(new Intent(getActivity(), MineWalletActivity.class));
                break;

        }
    }

    class GetHead extends AsyncTask<Void, Void, UserInfo> {

        private String c_name;
        private Bitmap bitmap;

        @Override
        protected UserInfo doInBackground(Void... params) {
            UserInfo users = BaseApplication.dbUserInfoController.findUsers();
            try {
                String head = users.getHead();
                byte[] datas = LoadDatas.getDatas(head);
                bitmap = BitmapFactory.decodeByteArray(datas, 0, datas.length);
                c_name = users.getName();
            } catch (NullPointerException e) {

            }

            return users;
        }

        @Override
        protected void onPostExecute(UserInfo userInfo) {
            super.onPostExecute(userInfo);
            head.setImageBitmap(bitmap);
            name.setText(c_name);
            info = userInfo;
        }
    }
}
